package com.ruoyi.factory.staffSafe.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.common.utils.IdGen;
import com.ruoyi.factory.staffSafe.domain.StaffType;
import com.ruoyi.factory.staffSafe.domain.StaffsafeCredential;
import com.ruoyi.factory.staffSafe.domain.StaffsafeInfo;
import com.ruoyi.factory.staffSafe.domain.StaffsafeRecord;
import com.ruoyi.factory.staffSafe.mapper.StaffTypeMapper;
import com.ruoyi.factory.staffSafe.mapper.StaffsafeCredentialMapper;
import com.ruoyi.factory.staffSafe.mapper.StaffsafeInfoMapper;
import com.ruoyi.factory.staffSafe.mapper.StaffsafeRecordMapper;
import com.ruoyi.factory.staffSafe.service.IStaffSafeService;
import com.ruoyi.factory.taxShare.service.impl.TaxReceiptServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName StaffSafeService
 * @Description: TODO 人员培训档案业务
 * @Author LiuJiaji
 * @Date 2024/4/12 16:12
 **/
@Service
public class StaffSafeServiceImpl implements IStaffSafeService
{

    protected final Logger logger = LoggerFactory.getLogger(StaffSafeServiceImpl.class);
    @Autowired
    private StaffsafeInfoMapper staffsafeInfoMapper;
    @Autowired
    private StaffsafeCredentialMapper staffsafeCredentialMapper;
    @Autowired
    private StaffsafeRecordMapper staffsafeRecordMapper;
    @Autowired
    private StaffTypeMapper staffTypeMapper;

    /**
     * @author: Liujiaji
     * @date: 2024/4/12 16:19
     * @description: TODO 定时任务同步erp员工数据
     * @param: []
     * @return: void
     **/
    @Override
    public void updateStaffInfo(){
        //删除现有员工数据
        staffsafeInfoMapper.deleteStaffsafeInfoAll();
        staffsafeCredentialMapper.deleteStaffsafeCredentialAll();
        //删除培训记录前两年
        StaffsafeRecord parst = new StaffsafeRecord();
        Calendar instance1 = Calendar.getInstance();
        instance1.add(Calendar.MONTH,-25);
        Date time = instance1.getTime();
        parst.setRecordtime(time);
        staffsafeRecordMapper.deleteStaffsafeRecordAll(parst);
        //查询erp员工信息更新到现有数据中
        StaffsafeInfo param = new StaffsafeInfo();
        List<StaffsafeInfo> staffsafeInfos = staffsafeInfoMapper.selectStaffsafeInfoListByErp(param);

        //插入数据
        for (StaffsafeInfo staffsafeInfo : staffsafeInfos) {
            try {
                staffsafeInfo.setDr("0");
                StaffsafeInfo staffsafeInfo1 = staffsafeInfoMapper.selectStaffsafeInfoById(staffsafeInfo.getId());
                if(null != staffsafeInfo1){
                    String education = staffsafeInfo.getEducation();
                    if(education.contains("本科")){
                        staffsafeInfoMapper.updateStaffsafeInfo(staffsafeInfo);
                    }
                }else{
                    staffsafeInfoMapper.insertStaffsafeInfo(staffsafeInfo);
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            }

            List<StaffsafeCredential> staffsafeCredentialList = staffsafeInfo.getStaffsafeCredentialList();

            //更新人员类别
            StaffType pst = new StaffType();
            pst.setUserid(staffsafeInfo.getId());
            StaffType staffType = staffTypeMapper.selectStaffTypeById(staffsafeInfo.getId());
            StaffType nstaffType = new StaffType();
            nstaffType.setUserid(staffsafeInfo.getId());
            nstaffType.setId(IdGen.uuid());
            nstaffType.setTypecode("3");

            StaffsafeCredential staffsafeCredential2 = staffsafeCredentialList.get(0);
            if(StringUtils.isNotEmpty(staffsafeCredential2.getCreid())){
                //有证人员
                nstaffType.setTypecode("2");
                if(null != staffType){
                    nstaffType.setId(staffType.getId());
                    //不为空判断是否是管理人员 不是0和1则设置2
                    String typecode = staffType.getTypecode();
                    if(!"0".equals(typecode) && !"1".equals(typecode)){
                        staffTypeMapper.updateStaffType(nstaffType);
                    }
                }else{
                    staffTypeMapper.insertStaffType(nstaffType);
                }
            }else{
                if(null != staffType){
                    nstaffType.setId(staffType.getId());
                    staffTypeMapper.updateStaffType(nstaffType);
                }else{
                    staffTypeMapper.insertStaffType(nstaffType);
                }
            }

            for (StaffsafeCredential staffsafeCredential : staffsafeCredentialList) {
                //插入资格证件信息
                String creid = staffsafeCredential.getCreid();
                if(StringUtils.isNotEmpty(creid)){
                    staffsafeCredential.setId(IdGen.uuid());
                    staffsafeCredential.setDr("0");
                    Date endtime = staffsafeCredential.getEndtime();
                    if(null != endtime){
                        Calendar instance = Calendar.getInstance();
                        instance.setTime(endtime);
                        instance.add(Calendar.MONTH,-3);
                        SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String format = simp.format(instance.getTimeInMillis());
                        staffsafeCredential.setReviewtime(format);
                    }
                    staffsafeCredentialMapper.insertStaffsafeCredential(staffsafeCredential);
                }
            }

        }
    }

}
